<template>
	<view class="category-container">
		<view class="category-header">
			<text class="page-title">图书分类</text>
		</view>
		
		<!-- 分类列表 -->
		<view class="category-list">
			<view class="category-item" v-for="(category, index) in categories" :key="index" @click="onCategoryClick(category)">
				<view class="category-info">
					<text class="category-name">{{category.name}}</text>
					<text class="category-count">{{category.bookCount || 0}}本</text>
				</view>
				<text class="iconfont icon-right"></text>
			</view>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading-box" v-if="loading">
			<text>加载中...</text>
		</view>
		
		<!-- 暂无数据提示 -->
		<view class="empty-box" v-if="!loading && categories.length === 0">
			<text>暂无分类数据</text>
		</view>
	</view>
</template>

<script>
	import { ref, onMounted } from 'vue';
	import { useBookStore } from '@/store/book.js';
	
	export default {
		setup() {
			// 状态定义
			const bookStore = useBookStore();
			const categories = ref([]);
			const loading = ref(false);
			
			// 页面初始化
			onMounted(() => {
				loadCategories();
			});
			
			// 加载分类数据
			async function loadCategories() {
				try {
					loading.value = true;
					const result = await bookStore.fetchCategories();
					categories.value = result || [];
				} catch (error) {
					console.error('加载分类失败:', error);
					uni.showToast({
						title: '加载分类失败',
						icon: 'none'
					});
				} finally {
					loading.value = false;
				}
			}
			
			// 点击分类
			function onCategoryClick(category) {
				uni.navigateTo({
					url: `/pages/book-list/book-list?categoryId=${category.id}&title=${encodeURIComponent(category.name)}`
				});
			}
			
			return {
				categories,
				loading,
				onCategoryClick
			}
		}
	}
</script>

<style lang="scss">
	.category-container {
		padding: 20rpx;
		background-color: #f8f8f8;
		min-height: 100vh;
		
		/* 页面标题 */
		.page-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
			padding: 20rpx 0;
		}
		
		/* 分类列表样式 */
		.category-list {
			background-color: #fff;
			border-radius: 12rpx;
			overflow: hidden;
			margin-top: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
			
			/* 分类项目 */
			.category-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx 20rpx;
				border-bottom: 1px solid #f0f0f0;
				
				&:last-child {
					border-bottom: none;
				}
				
				/* 分类信息 */
				.category-info {
					display: flex;
					flex-direction: column;
					
					.category-name {
						font-size: 32rpx;
						color: #333;
						margin-bottom: 6rpx;
					}
					
					.category-count {
						font-size: 24rpx;
						color: #999;
					}
				}
				
				/* 箭头图标 */
				.iconfont {
					color: #ccc;
					font-size: 32rpx;
				}
			}
		}
		
		/* 加载状态 */
		.loading-box {
			text-align: center;
			padding: 30rpx;
			color: #999;
		}
		
		/* 暂无数据提示 */
		.empty-box {
			text-align: center;
			padding: 100rpx 0;
			color: #999;
		}
	}
</style>
